package com.alibaba.citrus.cr.sales.plan.sdo;

import com.alibaba.citrus.cr.common.util.CalculatePercentage;
import com.alibaba.citrus.cr.sales.plan.model.features.SalesPlanPublishFeatures;

import lombok.Builder;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

@Data
@Builder
public class SalesPlanPublishSDO implements Serializable {
    private static final long serialVersionUID = 4632370146091386253L;

    private Long id;

    /**
     * 月度计划ID
     */
    private String planId;

    /**
     * 汇总类型2：货品维度3：组织维度4：类目维度
     */
    private Integer poolType;

    /**
     * 父级ID
     */
    private String parentId;

    /**
     * 1：业务员货品汇总 2：销售部门货品汇总 3：销售渠道货品汇总 4：销售组织货品汇总 5：业务单元货品汇总
     */
    private Integer bizType;

    /**
     * 数据维度ID
     */
    private String bizId;

    /**
     * 数据维度外部ID
     */
    private String bizOutId;

    /**
     * 货品ID
     */
    private String scItemId;

    /**
     * 货品组
     */
    private String scItemGroup;

    /**
     * 到岸价
     */
    private Long costInsuranceFreight;

    /**
     * 货品类目ID
     */
    private String scItemCategoryId;

    /**
     * 月份合同量
     */
    private Integer monthContractCount;

    /**
     * 折后合同金额
     */
    private Long monthContractMoney;

    /**
     * 折前合同金额
     */
    private Long beforeMonthContractMoney;

    /**
     * 计划量
     */
    private Integer monthCount;

    /**
     * 折后计划金额
     */
    private Long monthMoney;

    /**
     * 折前计划金额
     */
    private Long beforeMonthMoney;

    /**
     *  任务量
     */
    private Integer taskCount;

    /**
     * 折后任务金额
     */
    private Long taskMoney;

    /**
     * 折前任务金额
     */
    private Long beforeTaskMoney;

    /**
     * 合同差量 计划不同状态计算不一样
     */
    private Integer contractDifferenceCount;

    /**
     * 折后满足度
     */
    private BigDecimal contractSatisfaction;

    private BigDecimal beforeContractSatisfaction;

    /**
     * 调整量
     */
    private Integer adjustCount;

    /**
     * 折后调整金额
     */
    private Long adjustMoney;

    /**
     * 折前调整金额
     */
    private Long beforeAdjustMoney;

    /**
     * 上旬量
     */
    private Integer monthFirstCount;

    /**
     * 上旬回复量
     */
    private Integer monthFirstReturnCount;

    /**
     * 上旬任务量
     */
    private Integer monthFirstTaskCount;

    /**
     * 上旬调整量
     */
    private Integer monthFirstAdjustCount;

    /**
     * 上旬调整回复量
     */
    private Integer monthFirstAdjustReturnCount;

    /**
     * 上旬调整总量
     */
    private Integer monthFirstAdjustTotalCount;

    /**
     * 中旬量
     */
    private Integer monthMiddleCount;

    /**
     * 中旬回复量
     */
    private Integer monthMiddleReturnCount;

    /**
     * 中旬任务量
     */
    private Integer monthMiddleTaskCount;

    /**
     * 中旬调整量
     */
    private Integer monthMiddleAdjustCount;

    /**
     * 中旬调整回复量
     */
    private Integer monthMiddleAdjustReturnCount;

    /**
     * 中旬调整总量
     */
    private Integer monthMiddleAdjustTotalCount;

    /**
     * 下旬量
     */
    private Integer monthLastCount;

    /**
     * 下旬回复量
     */
    private Integer monthLastReturnCount;

    /**
     * 下旬任务量
     */
    private Integer monthLastTaskCount;

    /**
     * 下旬调整量
     */
    private Integer monthLastAdjustCount;

    /**
     * 下旬调整回复量
     */
    private Integer monthLastAdjustReturnCount;

    /**
     * 下旬调整总量
     */
    private Integer monthLastAdjustTotalCount;

    private Integer m1RollingPlanCount;

    private Integer m1RollingReturnCount;

    private Integer m2RollingPlanCount;

    private Integer m2RollingReturnCount;

    private Integer m3RollingPlanCount;

    private Integer m3RollingReturnCount;

    /**
     * 1：待锁定   2：待回复   3：待发布   4：已发布
     */
    private Integer status;


    private String features;


    private Integer version;


    private Date gmtCreate;


    private Date gmtModified;


    private String creator;


    private String updater;

    private SalesPlanPublishFeatures salesPlanPublishFeatures;


    /**
     * 计算折后任务金额
     * @param estimateDiscount  计划折后率
     */
    public void calculateTaskMoney(BigDecimal estimateDiscount){
        BigDecimal multiply = estimateDiscount.multiply(new BigDecimal("0.01").setScale(2, BigDecimal.ROUND_HALF_UP));
        BigDecimal subtract = new BigDecimal(1).subtract(multiply);
        this.setTaskMoney(new BigDecimal(this.getBeforeTaskMoney()).multiply(subtract).longValue());
    }

    /**
     * 计算计划发布任务量合同占比
     */
    public void calculateTaskSatisfaction(){
        this.contractDifferenceCount = this.getTaskCount() - this.getMonthContractCount();
        this.contractSatisfaction = CalculatePercentage.retainTwo(this.getTaskMoney(),this.getMonthContractMoney());
        this.beforeContractSatisfaction = CalculatePercentage.retainTwo(this.getBeforeTaskMoney(),this.getBeforeMonthContractMoney());
    }
}
